home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-06 | 5.5 KB | 202 lines | [TEXT/CWIE] |
- // Program Author: Paul Baxter
- // pbaxter@assistivetech.com
- //
- //
- #include <Speech.h>
- #include <Ctype.h>
- #include <DeskBus.h>
- #include <Retrace.h>
-
- #include "filter.h"
- #include "adb.h"
- #include "pref.h"
- #include "globals.h"
- #include "speech.h"
- #include "menu.h"
- #include "about.h"
- #include "command.h"
- #include "aerecord.h"
-
- typedef struct {
- long theMenuAndItem;
- CommandType command;
- } MenuCommand;
-
- #define MENU_ITEM(m,i) ((((unsigned long)m)<<16) | (i & 0xFFFF))
- #define kAnyMenuItem -1
-
- MenuCommand MenusCmds[] = {
- { MENU_ITEM(kAppleMenuID, kAboutItem), AboutCmd },
- { MENU_ITEM(kAppleMenuID, kAnyMenuItem), STRING_COMMAND(OpenDescAccCmd) },
- { MENU_ITEM(kFileMenuID, kFileMenuQuitItem), CHANGEVALUE(QuitCmd, kTrueValue) },
- { MENU_ITEM(kVoiceMenuID, kAnyMenuItem), STRING_COMMAND(VoiceCmd) },
- { MENU_ITEM(kSpeakMenuID, kSpeakCharsItem), TOGGLE_COMMAND(SpeakCharsCmd) },
- { MENU_ITEM(kSpeakMenuID, kSpeakWordsItem), TOGGLE_COMMAND(SpeakWordsCmd) },
- { MENU_ITEM(kSpeakMenuID, kSpeakSentenceItem), TOGGLE_COMMAND(SpeakSentencesCmd) },
- };
- #define kNumMenuCommands sizeof(MenusCmds)/sizeof(MenuCommand)
-
-
- // * ****************************************************************************** *
- // * MenusInit
- // * init our menus
- // * ****************************************************************************** *
- OSErr MenusInit(void)
- {
- MenuHandle themenu;
- VoiceDescription info;
- VoiceSpec voiceSpec;
- short count, voiceCount;
- OSErr err;
-
- InsertMenu(GetMenu(kAppleMenuID), 0);
- AppendResMenu(GetMenuHandle(kAppleMenuID), 'DRVR');
- InsertMenu(GetMenu(kFileMenuID), 0);
-
- // Build the Voice menu
- err = CountVoices(&voiceCount);
- if (!err && voiceCount) {
- themenu = GetMenu(kVoiceMenuID);
- InsertMenu(themenu, 0);
- for (count = 1; count <= voiceCount; count++) {
- err = GetIndVoice(count, &voiceSpec);
- if (err) continue;
-
- // get the name of the voice
- err = GetVoiceDescription(&voiceSpec, &info, sizeof(VoiceDescription));
- if (err) continue;
-
- // add the name to the menu
- if (themenu)
- AppendMenu(themenu, info.name);
-
- // Pick default voice
- if (EqualString(info.name,(**gPrefs).voice, false, false))
- gVoiceItem = count;
- }
-
- // Just in case our default is not installed
- if (gVoiceItem < 0)
- gVoiceItem = 1;
-
- // put a check mark for the voice name in the menu
- themenu = GetMenu(kVoiceMenuID);
- CheckItem(themenu, gVoiceItem, true);
- err = GetIndVoice(gVoiceItem, &voiceSpec);
- if (!err) {
- err = NewSpeechChannel(&voiceSpec, &gSpeechChannel);
- }
- }
- themenu = GetMenu(kSpeakMenuID);
- InsertMenu(themenu, 0);
-
- CheckItem(themenu, kSpeakCharsItem, (**gPrefs).speakChars);
- CheckItem(themenu, kSpeakWordsItem, (**gPrefs).speakWords);
- CheckItem(themenu, kSpeakSentenceItem, (**gPrefs).speakSentence);
-
- DrawMenuBar();
- return noErr;
- }
-
- // * ****************************************************************************** *
- // * SetDefaultVoice
- // * Set voice to string stored in pref
- // * ****************************************************************************** *
- OSErr SetDefaultVoice(void)
- {
- MenuHandle menuHandle;
- VoiceDescription info;
- VoiceSpec voiceSpec;
- short count, voiceCount;
- OSErr err;
-
- menuHandle = GetMenu(kVoiceMenuID);
- err = CountVoices(&voiceCount);
- if (!err && voiceCount) {
- for (count = 1; count <= voiceCount; count++) {
- err = GetIndVoice(count, &voiceSpec);
- if (err) continue;
-
- // get the name of the voice
- err = GetVoiceDescription(&voiceSpec, &info, sizeof(VoiceDescription));
- if (err) continue;
-
- // Pick default voice
- if (EqualString(info.name,(**gPrefs).voice, false, false)) {
- // avoid double takes from appleEvents
- if (count == gVoiceItem)
- return noErr;
-
- CheckItem(menuHandle, gVoiceItem, false);
- gVoiceItem = count;
-
- CheckItem(menuHandle, gVoiceItem, true);
- if (gSpeechChannel) {
- StopSpeech(gSpeechChannel);
- DisposeSpeechChannel(gSpeechChannel);
- gSpeechChannel = nil;
- }
- err = GetIndVoice(gVoiceItem, &voiceSpec);
- if (!err)
- err = NewSpeechChannel(&voiceSpec, &gSpeechChannel);
- return err;
- }
- }
- }
- return -1;
- }
-
-
- // * ****************************************************************************** *
- // * DoMenuItem
- // * Handle a menu action
- // * ****************************************************************************** *
- void DoMenuItem(long theMenuAndItem)
- {
- short theMenu, theItem, cmdMenu, cmdItem, titleLen, count;
- char buffer[sizeof(Str255) + sizeof(Str255)];
- Str255 theString;
- MenuHandle menuHandle;
- MenuCommand* curMenu;
- OSErr err;
-
- if (! theMenuAndItem) return;
-
- theMenu = HiWord(theMenuAndItem);
- theItem = LoWord(theMenuAndItem);
- menuHandle = GetMenuHandle(theMenu);
-
- // fill in buffer so we can speak menu title and menu item
- if (menuHandle) {
- titleLen = (**menuHandle).menuData[0];
- BlockMoveData(&((**menuHandle).menuData[1]), buffer, titleLen);
- GetMenuItemText(menuHandle, theItem, theString);
- }
- else {
- titleLen = 0;
- *buffer = 0;
- theString[0] = 0;
- }
-
- buffer[titleLen] = ' ';
- titleLen++;
- BlockMoveData(&theString[1], &buffer[titleLen], theString[0]);
- titleLen += theString[0];
-
- for (count = 0, curMenu = MenusCmds; count < kNumMenuCommands; curMenu++, count++) {
- cmdMenu = HiWord(curMenu->theMenuAndItem);
- if (cmdMenu == theMenu) {
- cmdItem = LoWord(curMenu->theMenuAndItem);
- if ((cmdItem == kAnyMenuItem) || (cmdItem == theItem)) {
- ProcessCommand(curMenu->command, theString);
- err = SavePrefs();
- if (gSpeechChannel)
- SpeakText(gSpeechChannel, buffer, titleLen);
- break;
- }
- }
- }
- HiliteMenu(0);
- DrawMenuBar();
- }
-